module necrophilia.helpers.ListArray;
import necrophilia.Exceptions;
import utils.Memory;

struct ListArray(T){
	private T[] _array;
	private uint _len=0;
	
	void setSize(uint ns){
		//writefln("new size: ",ns);
		_array.realloc(ns);
		if(_len>ns) _len=ns;
		//writefln("after new array.length:", _array.length, " ", _len);
	}
	//int huj;
	uint length(){
		return _len;
	}
	
	uint max(){
		return _array.length;
	}
	
	uint add(T node){
		//writefln("array.length:", _array.length, " ", _len," ",huj);
		//getchar();
		if(_len==_array.length){
			//writefln("array.length:", _array.length, " ", _len);
			throw new ListArrayException("cannot add another node because of no space");
		}
		_array[_len]=node;
		_len++;
		return _len;
	}
	
	void reset(){
		//huj++;
		//writefln("reset ok!");
		_len=0;
	}
	
	T[] get(){
		if(_len==0) return null;
		return _array[0.._len];
	}
}

import std.stdio;

unittest {
	alias ListArray!(int) myArray;
	{
		myArray a;
		a.setSize(5);
		assert(a.get is null);
		a.add(1);
		a.add(-1);
		assert(a.get.length==2);
		assert(a.get[1]==-1);
		assert(a.get[0]==1);
		a.reset;
		a.add(2);
		assert(a.get.length==1);
		assert(a.max==5);
		assert(a.get[0]==2);
		a.add(3);
		a.setSize(1);
		assert(a.max==1);
		assert(a.get.length==1);
		bool foo=false;
		try {
			a.add(6);
		} catch (ListArrayException e){
			foo=true;
		}
		assert(foo);
	}
}